package automaton;

import java.util.HashSet;
import java.util.Set;

public class State {
	
	private Automaton aut;
	private String name;
	private Set<StateTransition> transitions;
	
	private static int counter = 0;
	
	/* getter & setter */

	public Automaton getAut() {
		return aut;
	}

	public void setAut(Automaton aut) {
		this.aut = aut;
	}
	
	/* Konstruktor & create() */
	
	private State(Automaton aut) {
		this.setAut(aut);
		this.name = "z" + counter++;
		this.transitions = new HashSet<StateTransition>();
	}
	
	public String toString() {
		return this.name;
	}

	public static State create(Automaton aut){
		return new State(aut);
	}
	
	/* add(char by, State to)
	 * F??gt eine Transition von [this] mit [by] nach ??[to] in das TransitionsSet von this.Automaton hinzu!
	 */
	public void add(char by, State to, char output){
		StateTransition newTrans = StateTransition.create(this, by, to, output);
		this.getTransitions().add(newTrans);		
	}
	
	public Set<StateTransition> getTransitions(){
		return this.transitions;
	}
	
	public char getOutputAfter(State state){
		for(StateTransition current : this.getTransitions()){
			if(current.getTo().equals(state)) return current.getOutput();
		}
		throw new Error("State is not reachable!");
	}


	
}
